package com.example.demo_text;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AssignmentDAO {
    // Database connection string (using the same approach as other DAOs)
    private static final String JDBC_URL = databaseaccess.DatabaseUrl2;
    private static final String JDBC_USER = databaseaccess.DatabaseUser2;
    private static final String JDBC_PASSWORD = databaseaccess.DatabasePassword2;

    // Get a database connection
    private static Connection getConnection() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
        } catch (ClassNotFoundException e) {
            throw new SQLException("Database driver not found", e);
        }
    }

    // Close database resources
    private static void closeResources(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // Add a new assignment
    public static boolean addAssignment(int courseId, String assignmentTitle, String assignmentContent, 
                                      Date dueDate, int maxScore, boolean active) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "INSERT INTO assignments (course_id, assignment_title, assignment_content, due_date, max_score, active) " +
                         "VALUES (?, ?, ?, ?, ?, ?)";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, courseId);
            stmt.setString(2, assignmentTitle);
            stmt.setString(3, assignmentContent);
            stmt.setDate(4, dueDate);
            stmt.setInt(5, maxScore);
            stmt.setBoolean(6, active);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }

    // Get all assignments for a course
    public static List<Assignment> getAssignmentsByCourseId(String courseId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Assignment> assignments = new ArrayList<>();

        try {
            conn = getConnection();
            String sql = "SELECT * FROM assignments WHERE course_id = ? ORDER BY due_date";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, Integer.parseInt(courseId));
            rs = stmt.executeQuery();
            
            while (rs.next()) {
                Assignment assignment = new Assignment(
                    rs.getInt("assignment_id"),
                    rs.getInt("course_id"),
                    rs.getString("assignment_title"),
                    rs.getString("assignment_content"),
                    rs.getDate("due_date"),
                    rs.getInt("max_score"),
                    rs.getBoolean("active")
                );
                assignments.add(assignment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(conn, stmt, rs);
        }
        
        return assignments;
    }

    // Get a specific assignment by ID
    public static Assignment getAssignmentById(int assignmentId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = getConnection();
            String sql = "SELECT * FROM assignments WHERE assignment_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, assignmentId);
            rs = stmt.executeQuery();
            
            if (rs.next()) {
                return new Assignment(
                    rs.getInt("assignment_id"),
                    rs.getInt("course_id"),
                    rs.getString("assignment_title"),
                    rs.getString("assignment_content"),
                    rs.getDate("due_date"),
                    rs.getInt("max_score"),
                    rs.getBoolean("active")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeResources(conn, stmt, rs);
        }
        
        return null;
    }

    // Update assignment status (activate/deactivate)
    public static boolean updateAssignmentStatus(int assignmentId, boolean active) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "UPDATE assignments SET active = ? WHERE assignment_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setBoolean(1, active);
            stmt.setInt(2, assignmentId);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }

    // Delete an assignment
    public static boolean deleteAssignment(int assignmentId) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = getConnection();
            String sql = "DELETE FROM assignments WHERE assignment_id = ?";
            
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, assignmentId);
            
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            closeResources(conn, stmt, null);
        }
    }
} 